package com.soft.javase.day14.fileupload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/5/24
 */
public class FileUploadServer {
    public static void main(String[] args) {
        try (
                ServerSocket ss = new ServerSocket(6666)
        ) {
            System.out.println("服务器已启动，等待客户端连接");
            while (true) {
                try (
                        Socket socket = ss.accept();
                        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("java.png"))
                ) {
                    byte[] buffer = new byte[8192];
                    int len;
                    while ((len = bis.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                    bos.flush();
                    System.out.println("文件接收完成并上传至服务器");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
